package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.QuestionsAuditsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.QuestionsAudits;
import com.itheima.pojo.User;
import com.itheima.service.QuestionsAuditsService;
import com.itheima.vo.audits.QuestionsAuditsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionsAuditsServiceImpl implements QuestionsAuditsService {
    @Autowired
    private QuestionsAuditsMapper questionsAuditsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<QuestionsAuditsVO> findById(Integer id) {
        // 根据问题ID查询该问题审核意见对象集合
        LambdaQueryWrapper<QuestionsAudits> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsAudits::getQuestionsID, id);
        List<QuestionsAudits> auditsList = questionsAuditsMapper.selectList(wrapper);
        if (auditsList == null) {
            return null;
        }
        // 根据审核意见集合,填充对应的VO集合
        List<QuestionsAuditsVO> auditsVOS = new ArrayList<>();
        for (QuestionsAudits questionsAudits : auditsList) {
            QuestionsAuditsVO questionsAuditsVO = new QuestionsAuditsVO();
            questionsAuditsVO.setOpinion(questionsAudits.getRemarks());
            questionsAuditsVO.setChkTime(questionsAudits.getChkTime() + "");
            User user = userMapper.selectById(questionsAudits.getCheckerID());
            questionsAuditsVO.setChecker(user.getUsername());
            auditsVOS.add(questionsAuditsVO);
        }
        return auditsVOS;
    }
}
